package net.ib.mtalk.network.task;

import net.ib.mntalk.protocol.Common;
import net.ib.mntalk.protocol.Session.GetUserSettingReq;
import net.ib.mntalk.protocol.Session.GetUserSettingRes;
import net.ib.mntalk.protocol.Session.SetUserSettingRes;
import net.ib.mtalk.constant.HandlerMsgConst;
import net.ib.mtalk.network.TcpEnvelope;
import net.ib.mtalk.network.MethodId.Session;
import net.ib.mtalk.util.MTalkLogWriter;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

public class GetUserSettingTask extends AsyncNetTask{

	private GetUserSettingReq mGetUserSettingReq;
	
	public GetUserSettingTask(Context context, Handler uiHandler, GetUserSettingReq req) {
		super(context, uiHandler);
		mGetUserSettingReq = req;
	}

	protected TcpEnvelope onPreExecute() {
		TcpEnvelope rev = new TcpEnvelope.Builder()
		.setType(TcpEnvelope.REQUEST)
		.setMessage(mGetUserSettingReq)
		.setMethodId(Session.User.GetUserSetting)
		.build();
		return rev;
	}
	
	protected void onPostExecute(int resultCode, Object sentObject,
			Object receivedObject) {
		MTalkLogWriter.d("GetUserSettingTask.onPostExecute() resultCode : " + resultCode);
		if (receivedObject == null) {
			mUiHandler.sendMessageAtFrontOfQueue(mUiHandler.obtainMessage(HandlerMsgConst.HANDLER_UNKNOWN_ERROR));
			return;
		}
		
		TcpEnvelope resultEnvelope = (TcpEnvelope) receivedObject;
		GetUserSettingRes getUserSettingRes = (GetUserSettingRes) resultEnvelope.getMessage();
		
		MTalkLogWriter.d("GetUserSettingTask ResponseCode : " + getUserSettingRes.getResponseCode());
		
		if(getUserSettingRes.getResponseCode() == Common.SUCCESS){
			Message msg = mUiHandler.obtainMessage(HandlerMsgConst.HANDLER_GET_USER_SETTING_SUCCESS);
			msg.obj = getUserSettingRes.getSetting();
			mUiHandler.sendMessageAtFrontOfQueue(msg);
		} else {
				mUiHandler.sendMessageAtFrontOfQueue(mUiHandler.obtainMessage(HandlerMsgConst.HANDLER_GET_USER_SETTING_FAIL));
		}
	}
}
